package com.wanxi.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.education.mapper.CourseMapper;
import com.wanxi.education.model.pojo.Catalogue;
import com.wanxi.education.model.pojo.Course;
import com.wanxi.education.model.vo.advertandcertificate.ClassificationForCourseVo;
import com.wanxi.education.model.vo.catalogue.*;
import com.wanxi.education.model.vo.course.*;
import com.wanxi.education.service.ClassificationService;
import com.wanxi.education.service.CourseService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/9/24 0024
 * @Version: 1.0
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ClassificationService classificationService;

    @Override
    @Transactional
    public void add(CourseAddVo courseAddVo,Long adminId) {
        Course course = new Course();
        BeanUtils.copyProperties(courseAddVo,course);
        course.setAdminId(adminId);

        courseMapper.addCourseBasicInfo(course);

        courseMapper.addSaleAndPoint(course.getId());

        courseMapper.addCourseAndCcie(course.getId(),courseAddVo.getCcieId());

        List<CatalogueAddVo> catalogueAddVos = courseAddVo.getCatalogueAddVos();
//        BeanUtils.copyProperties(courseAddVo,catalogueAddVos);
        Catalogue catalogue = new Catalogue();
//        List<Catalogue> catalogueList = new ArrayList<>();
        for (CatalogueAddVo catalogueAddVo : catalogueAddVos ) {
            BeanUtils.copyProperties(catalogueAddVo,catalogue);
            catalogue.setCourseId(course.getId());

            courseMapper.addCatalogue(catalogue);

            List<CatalogueChildAddVo> catalogueChildAddVos = catalogueAddVo.getCatalogueChildAddVos();

            List<CatalogueChildInfoAddVo> catalogueChildInfoAddVos = new ArrayList<>();

            for (CatalogueChildAddVo childAddVo : catalogueChildAddVos) {
                CatalogueChildInfoAddVo childInfoAddVo = new CatalogueChildInfoAddVo();
                BeanUtils.copyProperties(childAddVo,childInfoAddVo);
                childInfoAddVo.setPid(catalogue.getId());
                childInfoAddVo.setCourseId(catalogue.getCourseId());
                childInfoAddVo.setAdminId(adminId);
                catalogueChildInfoAddVos.add(childInfoAddVo);
            }

            courseMapper.addChildCatalogues(catalogueChildInfoAddVos);

            courseMapper.addCatalogueVideoRelations(catalogueChildInfoAddVos);
        }
    }

    @Override
    public void updateStatusById(CourseStatusVo updateStatusById) {
        courseMapper.updateStatusById(updateStatusById);
    }

    @Override
    @Transactional
    public void updateBasicById(CourseBasicVo courseBasicVo) {
        courseMapper.updateCertificate(courseBasicVo.getId(),courseBasicVo.getCcieId());
        courseMapper.updateBasicById(courseBasicVo);
    }

    @Override
    public void updateInfoById(CourseInfoVo courseInfoVo) {
        courseMapper.updateInfoById(courseInfoVo);
    }

    @Override
    @Transactional
    public void updateCatalogueById(CatalogueUpdateVo catalogueUpdateVo) {
        courseMapper.updateCatalogueById(catalogueUpdateVo);
        if (catalogueUpdateVo.getVideoId()>0){
            courseMapper.updateCatalogueAndVideoById(catalogueUpdateVo);
        }
    }

    @Override
    public PageInfo<CourseVo> findPage(CoursePageVo coursePageVo) {
        PageHelper.startPage(coursePageVo.getPageNum(),coursePageVo.getPageSize());
        List<CourseVo> list = courseMapper.findAll(coursePageVo);
        PageInfo<CourseVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void deleteById(Long id) {
        courseMapper.deleteById(id);
    }

    @Override
    public void deleteCatalogueById(Long id) {
        courseMapper.deleteCatalogueById(id);
    }

    @Override
    public CatalogueInfoVo findCatalogueById(Long id) {
        CatalogueInfoVo catalogueInfoVo = courseMapper.findCatalogueById(id);
        return catalogueInfoVo;
    }

    @Override
    public List<CatalogueAllVo> findCatalogueAllById(Long id) {
        List<CatalogueAllVo> catalogueAllVo = courseMapper.findCatalogueAllById(id);
        return catalogueAllVo;
    }

    @Override
    public CourseInfoVo findCourseInfoById(Long id) {
        CourseInfoVo courseInfoVo = courseMapper.findCourseInfoById(id);
        return courseInfoVo;
    }

    @Override
    public CourseBasicVo findCourseBasicById(Long id) {
        CourseBasicVo courseBasicVo = courseMapper.findCourseBasicById(id);
        //查询对应的证书分类的信息
        ClassificationForCourseVo byCCIEId = classificationService.findByCCIEId(courseBasicVo.getCcieId());
        courseBasicVo.setCertificateNameVos(byCCIEId);
        return courseBasicVo;
    }

    @Override
    public List<CourseRecommendVo> findRecommends() {
        return courseMapper.findRecommends();
    }

    @Override
    public boolean updateCatalogueOrderById(CatalogueOrderVo catalogueOrderVo) {
        boolean byId = courseMapper.updateCatalogueOrderById(catalogueOrderVo);
        return byId;
    }

    @Override
    public boolean updateRecommendOrderById(CourseOrderVo courseOrderVo) {
        boolean byId =courseMapper.updateRecommendOrderById(courseOrderVo);
        return byId;
    }

    @Override
    @Transactional
    public boolean addRecommend(Long id) {
        int integer = courseMapper.countRecommend();
        if(integer>=6){
            return false;
        }
        int order = courseMapper.findRecommendMaxOrder();
        order++;
        try {
            boolean recommend = courseMapper.addRecommend(id,order);
            return recommend;
        }catch (DuplicateKeyException e){
//            e.printStackTrace();
        }
        return false;
    }

    @Override
    @Transactional
    public void addCatalogueById(CatalogueChildInfoAddVo catalogueChildInfoAddVo) {
//        boolean key = ObjectUtils.isEmpty(catalogueChildInfoAddVo.getVideoId());
        boolean key = catalogueChildInfoAddVo.getVideoId() != null;
        if (!key) {
            catalogueChildInfoAddVo.setPid(0L);
        }

        int order = courseMapper.findCatalogueMaxOrder(catalogueChildInfoAddVo.getPid());
        order++;
        catalogueChildInfoAddVo.setOrder(order);

        if (key) {
            courseMapper.addCatalogueById(catalogueChildInfoAddVo);
            courseMapper.addCatalogueVideoRelation(catalogueChildInfoAddVo);
            return;
        }
        courseMapper.addCatalogueById(catalogueChildInfoAddVo);
    }

    @Override
    public void deleteRecommend(Long id) {
        courseMapper.deleteRecommend(id);
    }

    @Override
    public Integer findCourseCountById(Long id) {
        return courseMapper.findCourseCountById(id);
    }

    @Override
    public BigDecimal findCoursePriceById(Long id) {
        return courseMapper.findCoursePriceById(id);
    }

    @Override
    public Integer findCourseSaleById(Long id) {
        return courseMapper.findCourseSaleById(id);
    }
}
